package org.ypx.center.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.ypx.center.service.IUserService;
import org.ypx.db.entity.*;
import org.ypx.db.mapper.PermissionMapper;
import org.ypx.db.mapper.RoleMapper;
import org.ypx.db.mapper.UserPermissionMapper;
import org.ypx.db.mapper.UserRoleMapper;
import org.ypx.shared.common.Message;
import org.ypx.shared.common.Result;
import org.ypx.shared.util.PasswordHelper;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by Yan Peixuan on 2017/3/17.
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private UserPermissionMapper userPermissionMapper;

    @Override
    public Result<User> loadUserByUsernameAndPassword(User user) {
        Result<User> result = new Result<User>();
        UserRole userRole = new UserRole();
        Role role = new Role();
        user = mapper.selectOne(user);
        if (user != null) {
            userRole.setUserId(user.getUserId());
            userRole = userRoleMapper.selectOne(userRole);
            role.setRoleId(userRole.getRoleId());
            role = roleMapper.selectOne(role);
            result.setData(user);
        } else {
            result.setMessage(Message.NO_USER);
        }
        return result;
    }

    @Override
    public void changePassword(User entity) {
        entity = PasswordHelper.encryptPassword(entity);
        /*XxxExample example = new XxxExample();
        Criteria  criteria = example.createCriteria();
        这个内部类中有对应Xxx的每个属性的条件方法 .
                假如 Xxx 包含id 属性.
        那它就有 andId(属性)EqualTo(SQL里的各种标准条件语句, =、!=、<、>、in、like、not之类的)的方法.
        criteria.andIdEqualTo(paramId).andAgeIn(Arrays.asList({10,20,30}));
        xxxMapper.updateByExampleSelective(xx,example);*/
        super.updateNotNull(entity);
    }

    @Override
    public User findByLoginName(String loginName) {
        User user = new User();
        user.setLoginName(loginName);
        user = mapper.selectOne(user);
        return user;
    }

    @Override
    public Set<String> findRoles(String loginName) {
        Set<String> stringSet = new HashSet<String>();
        User user = new User();
        user = findByLoginName(loginName);

        UserRole userRole = new UserRole();
        userRole.setUserId(user.getUserId());

        List<UserRole> userRoles = userRoleMapper.select(userRole);

        userRoles.forEach(userRole1 -> {
            Role role = new Role();
            role.setRoleId(userRole1.getRoleId());
            role = roleMapper.selectOne(role);
            stringSet.add(role.getRoleName());
        });
        return stringSet;
    }

    @Override
    public Set<String> findRight(String loginname) {
        Set<String> stringSet = new HashSet<String>();
        User user = new User();
        user = this.findByLoginName(loginname);

        UserPermission userRight = new UserPermission();
        userRight.setUserId(user.getUserId());
        List<UserPermission> userRights = userPermissionMapper.select(userRight);

        userRights.forEach(userRight1 -> {
            Permission permission = new Permission();
            permission.setPermissionId(userRight1.getPermissionId());
            permission = permissionMapper.selectOne(permission);
            stringSet.add(permission.getPermissionName());
        });
        return stringSet;
    }

    @Override
    public Result<List<User>> save(User entity) {
        entity = PasswordHelper.encryptPassword(entity);
        entity.setGenTime(new Date());
        entity.setState(1);
        return super.save(entity);
    }

    @Override
    public Result<List<User>> register(User entity) {
        entity = PasswordHelper.encryptPassword(entity);
        entity.setGenTime(new Date());
        entity.setState(1);
        if (!isSuccess(mapper.insert(entity))) {
            result.setMessage(Message.FAILE);
        }
        result.setMessage(Message.SUCCESS);
        return result;
    }

    @Override
    public void batchDelete(Long[] selectedIds) {
        User user = new User();
        for (Long oid : selectedIds) {
            user.setUserId(oid);
            this.delete(user);
        }
    }

    @Override
    public Result<List<User>> selectAll() {
        List<User> users = mapper.selectAll();
        Result<List<User>> results = new Result<>();
        results.setData(users);
        return results;
    }

    @Override
    public Result<List<User>> selectByExample(Object example) {
        Result<List<User>> results = new Result<>();
        List<User> users = mapper.select((User) example);
        results.setData(users);
        return results;
    }
}
